﻿using MyClassLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class FrmExamScheduleNew : Form
    {
        public FrmExamScheduleNew()
        {
            InitializeComponent();
        }
        MyDB.MedicareDataClassesDataContext DB = new MyDB.MedicareDataClassesDataContext();
        private void FrmNewSchedule_Load(object sender, EventArgs e)
        {
            var q2 = from p in DB.Patients
                     select new { p.姓名, p.病歷號碼, p.病患ID };

            BindingSource bs = new BindingSource();
            bs.DataSource = q2.ToList();
            comboBox1.DataSource = comboBox2.DataSource = bs;
            comboBox1.DisplayMember = "姓名";
            comboBox2.DisplayMember = "病歷號碼";
            comboBox1.ValueMember = "病患ID";

            BindingSource bsFrequency = new BindingSource();
            bsFrequency.DataSource = MyClassLibrary.frequency.GetFrequency();
            comboBox4.DataSource = bsFrequency;
            comboBox4.DisplayMember = "fName";

            comboBox3.DataSource = MyClassLibrary.Exam.GetExam();
            comboBox3.DisplayMember = "eName";

            RemindDays r = new RemindDays();
            comboBox6.DataSource = r.GetData();
            comboBox6.DisplayMember = "提早天數";
            comboBox6.ValueMember = "提醒天數ID";

            this.comboBox1.SelectedValueChanged += comboBox1_TextChanged;
            comboBox1_TextChanged(sender, e);

        }

        void comboBox1_TextChanged(object sender, EventArgs e)
        {
            var q = from p in DB.Diagnosis
                    where p.RegisterInformation.Patients.病患ID == (int)comboBox1.SelectedValue
                    orderby p.醫囑資料ID descending
                    select new { ID = p.醫囑資料ID };

            comboBox5.DataSource = q.ToList();
            comboBox5.DisplayMember = "ID";
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
           MyDB.ExamSchedule i= new MyDB.ExamSchedule
            {
                醫囑資料ID = int.Parse(comboBox5.Text),
                檢驗項目ID = ((Exam)comboBox3.SelectedItem).eID,
                檢驗頻率ID = ((frequency)comboBox4.SelectedItem).fID,
                起始日期 = 起始日期DateTimePicker.Value,
                結束日期 = 結束日期DateTimePicker.Value,
                備註 = 備註TextBox.Text,
                結案原因 = 結案原因TextBox.Text,
                結案 = 結案CheckBox.Checked
            };
           try
           {
               DB.ExamSchedules.InsertOnSubmit(i);
               DB.SubmitChanges();

               MessageBox.Show("新增成功!");
               this.Close();
           }
           catch (Exception)
           {
               MessageBox.Show("新增失敗");
           }


           int sID= DB.ExamSchedules.Select(n => n.排程ID).Max();

           for (DateTime d = 起始日期DateTimePicker.Value.Date; d < 結束日期DateTimePicker.Value.Date; d = d.AddDays(((frequency)comboBox4.SelectedItem).fDay))
           
           {
               MyDB.ExamCalendar ec = new MyDB.ExamCalendar
               {
                   排程ID = sID,
                   狀態ID = 2,
                   提醒天數ID = (int)comboBox6.SelectedValue,
                   檢驗結果ID = 1,
                   時段ID = 1,
                   日期 = d.Date,
                   備註 = 備註TextBox.Text,
                   提醒了沒 = false,
                   結果值 = "尚未檢測"

               };

               DB.ExamCalendars.InsertOnSubmit(ec);
           }
           try
           {
               DB.SubmitChanges();

               MessageBox.Show("已同步新增行事曆!");
               this.Close();
           }
           catch (Exception)
           {
               MessageBox.Show("行事曆同步失敗");
           }
           
            

           
            
           

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
